/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmachine.logic;

import java.util.Calendar;
import ticketmachine.dataObjects.DAOFactory;
import ticketmachine.dataObjects.IStationDAO;
import ticketmachine.dataObjects.ITicketDescriptionDAO;
import ticketmachine.dataObjects.IPromotionDAO;

/**
 *
 * @author oiiv2253
 */
public class TicketMachine {

    private int ticketsInMachine = 100;
    private int cashInMachine = 1000;
    private int localization;
    private Transaction currentTransaction;
    
    public TicketMachine() {
       
    }

    public void Inicialiate(){
    	ITicketDescriptionDAO ticketDescriptionDAO=DAOFactory.getInstance().getTicketDescriptionDAO();
    	IStationDAO stationDAO = DAOFactory.getInstance().getStationDAO();
    	IPromotionDAO promotionDAO = DAOFactory.getInstance().getPromotionDAO();

                // Ticket Descriptions
		
		ticketDescriptionDAO.createTicketDescription((float)10.0, "Single","ticketmachine.logic.priceStrategies.PriceCalculatorNormal");
		ticketDescriptionDAO.createTicketDescription((float)16, "Return","ticketmachine.logic.priceStrategies.PriceCalculatorNormal");
		ticketDescriptionDAO.createTicketDescription((float)60, "10 trip ticket","ticketmachine.logic.priceStrategies.PriceCalculatorNormal");
		ticketDescriptionDAO.createTicketDescription((float)15, "Weekly ticket","ticketmachine.logic.priceStrategies.PriceCalculatorWeek");
		ticketDescriptionDAO.createTicketDescription((float)30, "Monthly ticket","ticketmachine.logic.priceStrategies.PriceCalculatorNormal");
		ticketDescriptionDAO.createTicketDescription((float)200, "Annual ticket","ticketmachine.logic.priceStrategies.PriceCalculatorNormal");
		ticketDescriptionDAO.createTicketDescription((float)2.5, "Fun ticket", "ticketmachine.logic.priceStrategies.PriceCalculatorFamily");

                //TEST

                System.out.println("Ticket descriptions loaded:");

                for(TicketDescription t : ticketDescriptionDAO.getAllTicketDescriptions()) {
                    System.out.println(t.getId() + " " + t.getName() + " " + t.getPricingStrategyClass() );
                }

                // List of Stations
		
		stationDAO.createStation("Libcombe","11 Libcombe",10,2);
		stationDAO.createStation("Strathfield","22 Strathfield",1,3);
		stationDAO.createStation("Burwood","33 Burwood",2,4);
		stationDAO.createStation("Redfern","44 Redfern",3,5);
		stationDAO.createStation("Central","55 Central",4,6);
		stationDAO.createStation("Town Hall","66 Town Hall",5,7);
		stationDAO.createStation("Wynyard","77 Wynyard",6,8);
		stationDAO.createStation("Cicular Quay","88 Cicular Quay",7,9);
		stationDAO.createStation("St. James","99 St. James",8,10);
		stationDAO.createStation("Museum","110 Museum",9,1);

                //TEST

                System.out.println("Stations loaded:");

                for(Station s : stationDAO.listStations()) {
                    System.out.println(s.getId() + " " + s.getName() + " " + s.getAddress() );
                }

                //List of promotions

                

		promotionDAO.createPromotion("Student",10f,"1.1.2000","1.1.2100","ticketmachine.logic.priceStrategies.DiscountPerCentCalculatorStratagy");
                promotionDAO.createPromotion("Pensioner", 20f,"1.1.2000","1.1.2100","ticketmachine.logic.priceStrategies.DiscountPerCentCalculatorStratagy");
                promotionDAO.createPromotion("Unemployed", 20f,"1.1.2000","1.1.2100","ticketmachine.logic.priceStrategies.DiscountPerCentCalculatorStratagy");

                //TEST

                System.out.println("Promotions loaded:");

                for(Promotion p : promotionDAO.listPromotions()) {
                    System.out.println(p.getId() + " " + p.getName() + " " + p.getDiscountStrategyClass());
                }

		setLocalization(2);
		
	}

    public int getLocalization() {
        return localization;
    }

    public void setLocalization(int localization) {
        this.localization = localization;
    }

    public int getAmountOfTickets() {
        return ticketsInMachine;
    }

    public float getAmountOfCash() {
        return cashInMachine;
    }

    public void printTicket() {
        System.out.println("Printing ticket");
    }

    public void setTicketType(int ticketType) {

        Station s=DAOFactory.getInstance().getStationDAO().getStationById(this.localization);
    	TicketDescription d=DAOFactory.getInstance().getTicketDescriptionDAO().getDescriptionById(ticketType);
        currentTransaction.createNewTicket(d, s);

        //TEST

        System.out.println("Creating new ticket");
        System.out.println("Ticket source is: Station=" + s.getName());
        System.out.println("Selected ticket type is " + d.getName());
    }

    public void startNewTransAction(int transactionType) {

        currentTransaction = new Transaction(transactionType);
        
        //TEST
        System.out.println("New transaction started with type " + transactionType);
        System.out.println("Type 1 = Purchase");
        System.out.println("Type 2 = Reload");

    }


    public void setTicketDestination(int stationId) {

        Station destination = DAOFactory.getInstance().getStationDAO().getStationById(stationId);

        currentTransaction.getTicket().setDestination(destination);

        //TEST
        System.out.println("Ticket destination added");
        System.out.println("Ticket destination is " + destination.name);
    }
    
    

    public void applyTicketPromotion(int promotionId) {

    	if (promotionId!=-1){

                Promotion promotion = DAOFactory.getInstance().getPromotionDAO().getPromotionById(promotionId);
                
                 //TEST
                System.out.println("Adding promotion for ticket:");
                System.out.println("Promotion name " + promotion.getName());
                System.out.println("Amount of discount is " + promotion.getDiscount() + " %");

                currentTransaction.applyPromotionToTicket(promotion);

           
            
    	}    
    }

	public Transaction getCurrentTransaction() {
		return currentTransaction;
	}

	public void setCurrentTransaction(Transaction currentTransaction) {
		this.currentTransaction = currentTransaction;
	}

        //The number of family members for the family fun ticket
        public void setFamilySize (int familyMembers) {

            this.currentTransaction.ticket.setPrice(familyMembers);

        }
  
    
}
